package cn.zjj.pet.service.impl;

import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.org.domain.Department;
import cn.zjj.pet.domain.Pet;
import cn.zjj.pet.domain.PetType;
import cn.zjj.pet.mapper.PetTypeMapper;
import cn.zjj.pet.query.PetTypeQuery;
import cn.zjj.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        List<PetType> petTypes = petTypeMapper.selectAll();
        Map<Long, PetType> map = new HashMap<>();

        for (PetType petType : petTypes) {
            map.put(petType.getId(),petType);
        }
        List<PetType> typeTree = new ArrayList<>();
        for (PetType petType : petTypes) {
            if(petType.getPid()==null){
                typeTree.add(petType);
            }else {
                PetType parentPetType = map.get(petType.getPid());

                parentPetType.getChildren().add(petType);
            }
        }
        return typeTree;
    }
}
